//
//  HomeViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2017/6/27.
//  Copyright © 2017年 吴斌. All rights reserved.
//

#import "HomeViewController.h"
#import "NSObject+AFNetworking.h"

#import "ManageMoneyCell.h"
#import "HomeSectionTitle.h"
#import "BorrowOrInvestCell.h"
#import "ActivateBorrowView.h"
#import "CycleScrollViewCell.h"
#import "PlatformDataCell.h"
#import "HomeFootView.h"
#import "LoginViewController.h"
#import "XYHomeNavigationView.h"

//首页导航栏滚动距离
#define NAVBAR_CHANGE_POINT 50

static NSString * ManagerMoneyCellReuseId = @"ManageMoneyCell";
static NSString * BorrowOrInvestCellReuseId = @"BorrowOrInvestCell";
static NSString * CycleScrollViewCellReuseId = @"CycleScrollViewCell";
static NSString * PlatformDataCellReuseId = @"PlatformDataCell";

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource,ActivateBorrowViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) ActivateBorrowView *headView;

/** <#注释#> */
@property (nonatomic, strong) HomeFootView *footView;
/** <#注释#> */
@property (nonatomic, strong) XYHomeNavigationView *naviView;

@end

@implementation HomeViewController
#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"幸运贷";
//    [self registerCell];
    [self.view addSubview:self.tableView];
    self.navigationController.navigationBar.hidden = YES;
    self.wb_leftButton.hidden = YES;
    
    XYHomeNavigationView *naviView = [[XYHomeNavigationView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavigationBarHeight)];
    [naviView.leftBtn removeFromSuperview];
    _naviView = naviView;
    [self.view addSubview:naviView];
    
//    [Factory getUesrInfo];
}



- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:ManagerMoneyCellReuseId bundle:nil] forCellReuseIdentifier:ManagerMoneyCellReuseId];
    [self.tableView registerClass:[CycleScrollViewCell class] forCellReuseIdentifier:CycleScrollViewCellReuseId];
    [self.tableView registerClass:[BorrowOrInvestCell class] forCellReuseIdentifier:BorrowOrInvestCellReuseId];
    [self.tableView registerClass:[PlatformDataCell class] forCellReuseIdentifier:PlatformDataCellReuseId];    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return section == 2 ? 1 : 2;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
           BorrowOrInvestCell *cell = [tableView dequeueReusableCellWithIdentifier:BorrowOrInvestCellReuseId forIndexPath:indexPath]; 
            WB_WeakSelf(self)
            cell.clickMethod = ^(UIView *view) {
                DebugLog(@"店家了");
                if (view.tag == 1000) {
                    if (![Factory isLogin]) {
                        [Factory loadLoginScreen];
                    }else{
                        //激活处理
                    }
                }else if(view.tag == 1001){
                    weakself.tabBarController.selectedIndex = 1;
                }
            };
            return cell;
        }else{
           CycleScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CycleScrollViewCellReuseId forIndexPath:indexPath];
            return cell;
        }   
    }else if(indexPath.section == 1){
        ManageMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:ManagerMoneyCellReuseId forIndexPath:indexPath];
        cell.titleLb.text = @[@"省心投资",@"专业投资"][indexPath.row];
        cell.detailLb.text = @[@"固定收益 平台赔付",@"灵活选标 挑战更高收益"][indexPath.row];
        
        cell.titleLb.font = [UIFont boldSystemFontOfSize:16];
        cell.titleLb.textColor = WB_RGBColor(51, 51, 51);
        
        cell.describLb.textColor = WB_RGBColor(153, 153, 153);
        cell.describLb.font = [UIFont systemFontOfSize:12];
        
        cell.detailLb.textColor = WB_RGBColor(153, 153, 153);
        cell.detailLb.font = [UIFont systemFontOfSize:12];
        
        NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@[@"9.5",@"10-24"][indexPath.row]];
        NSDictionary * firstAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:22],NSForegroundColorAttributeName:WB_RGBColor(255, 78, 78)};
        [firstPart setAttributes:firstAttributes range:NSMakeRange(0,firstPart.length)];
        NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@"%"];
        NSDictionary * secondAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:WB_RGBColor(255, 78, 78),};
        [secondPart setAttributes:secondAttributes range:NSMakeRange(0,secondPart.length)];
        [firstPart appendAttributedString:secondPart];
        cell.dataLb.attributedText = firstPart;
        cell.describLb.text = @"年化收益率";
        if (indexPath.row == 1) {
            cell.line.hidden = YES;
        }
        return cell;
    }else if(indexPath.section == 2){
        ManageMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:ManagerMoneyCellReuseId forIndexPath:indexPath];
        cell.line.hidden = YES;
        
        cell.dataLb.textColor = WB_RGBColor(153, 153, 153);
        cell.dataLb.font = [UIFont systemFontOfSize:12];
        cell.dataLb.text = @"累计注册用户(人)";
        
        cell.titleLb.font = [UIFont systemFontOfSize:12];
        cell.titleLb.textColor = WB_RGBColor(153, 153, 153);
        cell.titleLb.text = @"累计成交金额(元)";
        
        cell.detailLb.textColor = WB_RGBColor(53, 134, 225);
        cell.detailLb.font = [UIFont boldSystemFontOfSize:20];
        cell.detailLb.text = @"637万";
        
        cell.describLb.textColor = WB_RGBColor(53, 134, 225);
        cell.describLb.font = [UIFont boldSystemFontOfSize:20];
        cell.describLb.text = @"12,899万";
        
        return cell;
    }else{
        return nil;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HomeSectionTitle *view = [[NSBundle mainBundle] loadNibNamed:@"HomeSectionTitle" owner:nil options:nil].firstObject;
    if (section == 2) {
        view.titleLb.text = @"平台数据";
    }else if (section == 1) {
        view.titleLb.text = @"散标理财";
    }
    view.frame = CGRectMake(0, 0, kScreenWidth, 30);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (section == 0) ? 0.f : 30.f + 20.0f; 
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 72.0f;
        }else{
            return 125.0f;
        }
    }
    return 75.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            self.tabBarController.selectedIndex = 1;
        }else{
            self.tabBarController.selectedIndex = 1;
        }
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat cellOffy = scrollView.contentOffset.y;
    if (cellOffy < 0) {
        self.naviView.hidden = YES;
    }else{
        self.naviView.hidden = NO;
    }
    
    if (cellOffy > NAVBAR_CHANGE_POINT) {
        
        [self.naviView changeColorWithState:YES];
        self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, 0.8);
        [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleDefault animated:true];
    }else {
        CGFloat alpa = cellOffy/NAVBAR_CHANGE_POINT;
        if (alpa > 0.8) {
            self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, 0.8);
            return ;
        }
        [self.naviView changeColorWithState:NO];
        self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, alpa);
        [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleLightContent animated:true];
    }
}

#pragma mark - ActiveView Delegate
- (void)clickBorrowView:(ActivateBorrowView *)view button:(UIButton *)button
{
    if (![Factory isLogin]) {
        [Factory loadLoginScreen];
    }else{
        //登录后进入进件页面
    }
}

#pragma mark - 懒加载  Lazy Load
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabbarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = WB_RGBColor(245, 248, 253);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableView registerNib:[UINib nibWithNibName:ManagerMoneyCellReuseId bundle:nil] forCellReuseIdentifier:ManagerMoneyCellReuseId];
        [_tableView registerClass:[CycleScrollViewCell class] forCellReuseIdentifier:CycleScrollViewCellReuseId];
        [_tableView registerClass:[BorrowOrInvestCell class] forCellReuseIdentifier:BorrowOrInvestCellReuseId];
        [_tableView registerClass:[PlatformDataCell class] forCellReuseIdentifier:PlatformDataCellReuseId];  
        
    }
    return _tableView;
}

- (ActivateBorrowView *)headView
{
    if (_headView == nil) {
        _headView = [[ActivateBorrowView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 215 + kIPhoneXMoreHeight)];
        _headView.delegate = self;
    }
    return _headView;
}

- (HomeFootView *)footView
{
    if (_footView == nil) {
        _footView = [[NSBundle mainBundle] loadNibNamed:@"HomeFootView" owner:self options:nil].firstObject;
        _footView.frame = CGRectMake(0, 0, kScreenWidth, 80);
    }
    return _footView;
}


@end
